package com.soft.mainsite.module.api;

import com.soft.mainsite.common.constant.CmsConst;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.city.dto.CityDto;
import com.soft.mainsite.module.city.service.CityService;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import com.soft.mainsite.mybatis.model.TCmsSite;
import com.soft.mainsite.common.annotation.ParamNotNull;
import com.soft.mainsite.common.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:内容栏目接口
 *
 * @author Jin
 * @create 2017-09-09
 **/
@RestController
@RequestMapping("/api/category")
public class CategoryApiController {

    @Autowired
    SiteService siteService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    private CityService cityService;

    @GetMapping(value = "/list")
    @ParamNotNull(parameter = "siteId,categoryId")
    public String list(@RequestParam("siteId") Integer siteId,
                       @RequestParam("categoryId") Long categoryId,
                       @RequestParam(value = "isNav", defaultValue = "0") Boolean isNav
    ) {
        TCmsSite site = siteService.findById(siteId);
        if (CmsUtil.isNullOrEmpty(site)) {
            throw new ApiException("[" + siteId + "]" + CmsConst.SITE_NOT_FOUND);
        }
        List<TCmsCategory> cats = categoryService.findCategoryListByPidAndIsNav(categoryId, siteId, isNav);
        if (CmsUtil.isNullOrEmpty(cats)) {
            TCmsCategory category = categoryService.findById(categoryId);
            if (CmsUtil.isNullOrEmpty(category)) {
                throw new ApiException("没有查询到数据！[siteId:" + siteId + ",categoryId:" + categoryId + "]");
            }
            cats = categoryService.findCategoryListByPidAndIsNav(category.getParentId(), siteId, isNav);
        }

        return JsonUtil.toSuccessResultJson("请求成功", cats);
    }

    @GetMapping("/{categoryId}")
    public String content(@PathVariable("categoryId") Long categoryId,
                          @RequestParam(value = "isParent", defaultValue = "0") Integer isParent) {
        TCmsCategory category = categoryService.findById(categoryId);
        if (CmsUtil.isNullOrEmpty(category)) {
            throw new ApiException("栏目[" + categoryId + "]不存在！");
        }
        if (isParent == 1 && category.getParentId() != 0) {
            category = categoryService.findById(category.getParentId());
        }
        return JsonUtil.toSuccessResultJson("请求成功", category);
    }

    @RequestMapping("/findSiteName")
    @ResponseBody
    public String findSiteName(@RequestParam(value = "domain") String siteDomain) {
        TCmsSite tCmsSite = siteService.findByDomain(siteDomain);
        if (!CmsUtil.isNullOrEmpty(tCmsSite)) {
            Map<String, Object> rMap = new HashMap<>(16);
            CityDto cityDto = cityService.getCityBySiteId(tCmsSite.getSiteId());
            if (!CmsUtil.isNullOrEmpty(cityDto)) {
                rMap.put("cityName", cityDto.getCityName());
            }
            rMap.put("siteName", tCmsSite.getSiteName());
            rMap.put("siteId", tCmsSite.getSiteId());
            return JsonUtil.toSuccessResultJson("请求成功", rMap);
        }
        return JsonUtil.toError("站点不存在");
    }
}
